package com.nerve.android.cloudoffice.app;

import org.nerve.android.NerveActivity;
import org.nerve.android.NerveActivityGroup;

import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.nerve.android.cloudoffice.OfficeApplication;
import com.nerve.android.cloudoffice.R;


public abstract class NavigationActivity extends NerveActivity{
	/*
	 * 工作者线程的返回值定义
	 */
	public static final int WORKER_OK = 10001;
	public static final int WORKER_FAIL = 10002;
	
	/*
	 * startActivityForResult 中的code
	 */
	public static final int CODE_LOGIN = 101;
	public static final int CODE_REGISTER = 102;
	public static final int CODE_FIND = 103;
	
	private static final String TAG = "NavigationActivity";
	
	/*
	 * 整体的布局
	 */
	protected RelativeLayout mainLayout;
	/*
	 * 导航的布局
	 */
	protected RelativeLayout navLayout;
	
	/*
	 * 导航条中的元素
	 */
	protected ImageView navBackBtn;
	protected Button navRightBtn;
	protected TextView navTitle;
	private View.OnClickListener navButtonListener;
	
	protected int contentLayout;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.nav_main);
        this.mainLayout = (RelativeLayout)findViewById(R.id.main_view);
        buildNavigate();
        
        super.onCreate(savedInstanceState);
    }
	
	protected void buildUI(){
    	loadView();
    }
	
	/**
	 * 增加子视图到总视图下
	 */
	protected void loadView(){
		if(this.mainLayout == null)
			return ;
		
		if(this.contentLayout > 0){
			View contentView = View.inflate(this, contentLayout, null);
	    	Log.d(TAG, "加载视图ok =" + contentView);
	    	
	    	RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
	    				LayoutParams.FILL_PARENT, 
	    				LayoutParams.FILL_PARENT
	    			);
	    	lp.addRule(RelativeLayout.BELOW, R.id.navigationBar);
	    	
	    	mainLayout.addView(contentView, lp);
		}
	}
	
	/**
	 * 重写父类的loadView方法
	 */
	@Override
	protected void loadView(int id) {
		this.contentLayout = id;
		loadView();
		System.out.println("加载 view 完成!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
	}
	
    /*
     * ================================================================
     * BEGIN 导航栏相关方法
     * ================================================================
     */
	
	/**
     * 构建导航栏
     */
    protected void buildNavigate(){
    	System.out.println("===================================================================");
    	this.navLayout = (RelativeLayout)findViewById(R.id.navigationBar);
    	this.navBackBtn = (ImageView)findViewById(R.id.navLeftBtn);
    	this.navRightBtn = (Button)findViewById(R.id.navRightBtn);
    	this.navRightBtn.setVisibility(View.GONE); //默认不显示右边的按钮
    	//this.navRightBtn.getPaint().setFakeBoldText(true);
    	this.navTitle = (TextView)findViewById(R.id.navTitle);
    	
    	this.navButtonListener = new View.OnClickListener() {
    		@Override
    		public void onClick(View v) {
    			if(v.getId() == R.id.navLeftBtn){
    				onNavigateBackButtonClick();
    			}
    			if(v.getId() == R.id.navRightBtn) {
    				onNavigateRightButtonClick();
    			}else{
    				onNavigateTitleClick();
    			}
    		}
    	};
    	
    	this.navBackBtn.setOnClickListener(navButtonListener);
    	this.navRightBtn.setOnClickListener(navButtonListener);
    	this.navTitle.setOnClickListener(navButtonListener);
    	
    	Log.d(TAG, "初始化导航栏ok");
    }
    
    protected void showNavigateButton(boolean showBack, boolean showRight){
    	this.navBackBtn.setVisibility(showBack ? View.VISIBLE : View.GONE);
    	this.navRightBtn.setVisibility(showRight ? View.VISIBLE : View.GONE);
    }
    
    protected void showNavigateBackButton(boolean showBack){
    	this.navBackBtn.setVisibility(showBack ? View.VISIBLE : View.GONE);
    }

    protected void showNavigate(boolean isShow){
    	this.navLayout.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }
    
    /**
     * 结束当前的activity，返回到上一个活动
     */
    protected void onNavigateBackButtonClick(){
    	this.setResult(RESULT_OK);
    	this.finish();
    }
    
    /**
     * 设置右边导航按钮的文字
     *	@param title
     */
    protected void customNavigateRightButton(String title, boolean enable){
    	this.navRightBtn.setText(title);
    	this.navRightBtn.setEnabled(enable);
    	this.navRightBtn.setVisibility(View.VISIBLE);
    }
    
    /**
     * 当左边的导航栏被点击时触发
     * 
     * 子类可以重写这个方法达到不同的效果
     */
    protected void onNavigateRightButtonClick(){
    	Log.d(TAG, "左边的导航栏被点击了@");
    }
    
    /**
     * @method name: onNavigateTitleClick
     * @description: 当中间的标题栏被点击
     * @return type: void
     *
     */
    protected void onNavigateTitleClick(){
    }
    
    /**
     * 设置导航标题
     * @param title
     */
    protected void setNavTitle(String title){
    	if(this.navTitle != null)
    		this.navTitle.setText(title);
    }
    
    protected void setNavTitle(String title, int icon){
    	setNavTitle(title);
    	
    	Drawable autoDrawable = getResources().getDrawable(icon);
		autoDrawable.setBounds(0, 0, autoDrawable.getMinimumWidth(), autoDrawable.getMinimumHeight());
		navTitle.setCompoundDrawablePadding(10);
		navTitle.setCompoundDrawables(null, null, autoDrawable, null);
    }
    /*
     * ================================================================
     * END 导航栏相关方法
     * ================================================================
     */
	
	
	/**
     * 跳转到指定的Actitivy
     *	@param target	target Activity
     *	@param isClear	define if clear top Activity stack
     */
    protected void toActivity(Class<?> target, boolean isClear){
    	Intent intent = getIntent();
    	intent.setClass(this, target);
    	if(isClear)
    		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    	
    	startActivity(intent);
    }
    
    
    /**
     * 得到一个ActivityGroup，这个需要parent为ActivityGroup才有效
     *	@param cls
     *	@return
     */
    protected NerveActivityGroup getActivityGroup(Class<? extends ActivityGroup> cls){
    	try{
    		return (NerveActivityGroup)cls.cast(getParent());
    	}catch(Exception e){
    		e.printStackTrace(System.out);
    		return null;
    	}
    }
    
    
    protected void putData(String key, Object value){
    	OfficeApplication app = (OfficeApplication)getApplication();
		app.putData(key, value);
	}
	protected Object getData(String key, boolean isClear){
		OfficeApplication app = (OfficeApplication)getApplication();
		return app.getData(key, isClear);
	}
}